package com.sense.kukoo.common.wiget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.sense.kukoo.common.R;

import java.util.ArrayList;

/**
 * <br> ClassName:   BottomTabLayout.java
 * <br> Description:  主界面底部選項卡封裝
 * <br> Author:      lionszhang
 * <br> Date:        2022/1/6
 */
public class BottomTabLayout extends TabLayout {

    private OnTabListener mOnTabListener;
    private String[] mTitles;
    private int[]mIconSels;
    private int[]mIconNols;

    private ArrayList<Tab> mTabs = new ArrayList<Tab>();
    private boolean isShowFollowTabReddot = false;

    public interface OnTabListener {
        void onTabListener(int position);
    }

    public void setOnTabListener(OnTabListener onTabListener) {
        this.mOnTabListener = onTabListener;
    }

    public BottomTabLayout(Context context) {
        super(context);
    }

    public BottomTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BottomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * @Author Lionszhang
     * @Date 2021/4/14 9:05
     * @Instruction 初始化數據
     */
    public void init(String[] titles,int[]iconSels,int[]iconNols) {
        this.mIconNols=iconNols;
        this.mIconSels=iconSels;
        this.mTitles=titles;
        this.addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    TextView tab_layout_text = tab.getCustomView().findViewById(R.id.tab_layout_text);
                    ImageView icon = tab.getCustomView().findViewById(R.id.tab_layout_icon);
                    //
                    tab_layout_text.setTextColor(getResources().getColor(com.sense.kukoo.common_design.R.color.text_contrast));
                    icon.setImageResource(mIconSels[tab.getPosition()]);
                    if (mOnTabListener!=null){
                        mOnTabListener.onTabListener(tab.getPosition());
                    }
                }
            }

            @Override
            public void onTabUnselected(Tab tab) {
                if (tab != null && tab.getCustomView() != null) {
                    TextView tab_layout_text = tab.getCustomView().findViewById(R.id.tab_layout_text);
                    ImageView icon = tab.getCustomView().findViewById(R.id.tab_layout_icon);
                    //
                    tab_layout_text.setTextColor(getResources().getColor(com.sense.kukoo.common_design.R.color.text_secondary));
                    icon.setImageResource(mIconNols[tab.getPosition()]);
                }
            }

            @Override
            public void onTabReselected(Tab tab) {}
        });
    }

    /**
     * @Author Lionszhang
     * @Date 2021/4/14 9:06
     * @Para tab 选项卡tab,position 位置,titles 选项卡标题
     * @Instruction 设置选项卡标题
     */
    public void setTab(Tab tab, int position) {
        mTabs.add(position, tab);
        tab.setCustomView(R.layout.common_lib_tab_view);
        if (tab.getCustomView() != null) {
            TextView tab_layout_text = tab.getCustomView().findViewById(R.id.tab_layout_text);
            ImageView icon = tab.getCustomView().findViewById(R.id.tab_layout_icon);
            //
            tab_layout_text.setText(mTitles[position]);
            tab_layout_text.setTextColor(getResources().getColor(R.color.common_text_gray_1));
            icon.setImageResource(mIconNols[position]);
        }
    }

    public void setFollowTabReddot(boolean isShow){
        if(isShowFollowTabReddot == isShow){
            return;
        }
        isShowFollowTabReddot = isShow;
        //
        View customView = mTabs.get(1).getCustomView();
        if (customView != null) {
            View tab_layout_reddot = customView.findViewById(R.id.tab_layout_reddot);
            if (tab_layout_reddot != null) {
                tab_layout_reddot.setVisibility(isShow ? View.VISIBLE : View.INVISIBLE);
            }
        }
    }

}
